# This Makefile was tested with GNU Make

# Use pkg-config to lookup the proper compiler and linker flags for LCM
CFLAGS=`pkg-config --cflags lcm`
LDFLAGS=`pkg-config --libs lcm glib-2.0 gthread-2.0`

# list the LCM types here
lcmtypes=example_t

# derive the file names of the .c, .h, and .o files corresponding
# to the LCM types here
lcm_c_h_files=$(lcmtypes:%=%.c) $(lcmtypes:%=%.h)
lcm_obj_files=$(lcmtypes:%=%.o)

# compile the LCM types to a static library 
all: libtypes.a listener send-video

listener: listener.o libtypes.a decode_video.o image_handler.o
	$(CC) -o $@ $^ $(LDFLAGS) -lavutil -lavformat -lavcodec -lz -lavutil -lm

send-video: send_message.o libtypes.a send_video.o 
	$(CC) -o $@ $^ $(LDFLAGS)

# prevent auto-generated lcm .c/.h files from being deleted
.SECONDARY : $(lcm_c_h_files)

libtypes.a: $(lcm_obj_files)
	ar rc $@ $(lcm_obj_files)

%.o: %.c %.h
	gcc $(CFLAGS) -c $< 

decode_video.o: decode_video.c
	gcc -c decode_video.c -o decode_video.o 

image_handler.o: image_handler.c
	gcc -c image_handler.c -o image_handler.o

send_video.o: send_video.c
	gcc -c send_video.c -o send_video.o

%.c %.h: ../types/%.lcm
	lcm-gen -c $<

clean:
	rm -f listener send-video read-log
	rm -f *.o
	rm -f libtypes.a
	rm -f $(lcm_c_h_files)
	rm -f duration.txt

# modifications to this makefile
# - removed log-gen ... we dont use it
# - added decode_video.o and required FFMPEG libraries to the listener rule
# - added new rule for decode_video.o to compile source into object file

